package com.java110.mall.cart.bmo.housekeepingServFee.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.mall.cart.bmo.housekeepingServFee.ISaveHousekeepingServFeeBMO;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;

import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.po.housekeeping.HousekeepingServFeePo;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("saveHousekeepingServFeeBMOImpl")
public class SaveHousekeepingServFeeBMOImpl implements ISaveHousekeepingServFeeBMO {

    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;
    /**
     * 添加小区信息
     *
     * @param housekeepingServFeePo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(HousekeepingServFeePo housekeepingServFeePo, JSONObject reqJson) {

        String servId = reqJson.getString( "servId" );

        if(null == servId || "".equals( servId )){
            HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
            housekeepingServDto.setHktId(reqJson.getString("hktId"));
            housekeepingServDto.setShopId(reqJson.getString("shopId"));
            List<HousekeepingServDto> housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
            if(null != housekeepingServDtos && housekeepingServDtos.size() > 0){
                for(HousekeepingServDto ServDto: housekeepingServDtos){
                    housekeepingServFeePo.setServId(ServDto.getServId());
                    housekeepingServFeePo.setServName(ServDto.getServName());
                    housekeepingServFeePo.setServFeeId(GenerateCodeFactory.getGeneratorId("11"));
                    int flag = housekeepingServFeeInnerServiceSMOImpl.saveHousekeepingServFee(housekeepingServFeePo);
                    if (flag < 1) {
                        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
                    }
                }
            }
        }else {
            String servName = reqJson.getString( "servName" );
            housekeepingServFeePo.setServId( servId );
            housekeepingServFeePo.setServName( servName );
            housekeepingServFeePo.setServFeeId(GenerateCodeFactory.getGeneratorId("11"));
            int flag = housekeepingServFeeInnerServiceSMOImpl.saveHousekeepingServFee(housekeepingServFeePo);
            if (flag < 1) {
                return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
            }
        }


        return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
    }

}
